package org.dreamwork.jasmine2.sample.converter;

import org.dreamwork.util.*;

import java.io.*;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: seth.yang
 * Date: 11-8-4
 * Time: 下午4:53
 */
public class Test {
    private String name;
    @ConverterInfo (type = Class1.class)
    private Class1[] first;
    @ConverterInfo (type = Class2.class)
    private List<Class2> second;
    @ConverterInfo (
            type = ICollection.class,
            xmlConverter = CollectionConverter.class,
            jsonConverter = CollectionConverter.class
    )
    private ICollection<Class1> data;
    @ConverterInfo (type = Map.class, xmlConverter = MapConverter.class)
    private Map<String, String> map;

    public Class1[] getFirst () {
        return first;
    }

    public void setFirst (Class1[] first) {
        this.first = first;
    }

    public String getName () {
        return name;
    }

    public void setName (String name) {
        this.name = name;
    }

    public List<Class2> getSecond () {
        return second;
    }

    public void setSecond (List<Class2> second) {
        this.second = second;
    }

    public ICollection<Class1> getData () {
        return data;
    }

    public void setData (ICollection<Class1> data) {
        this.data = data;
    }

    public Map<String, String> getMap () {
        return map;
    }

    @ConverterInfo (type = Map.class, xmlConverter = MapConverter.class)
    public void setMap (Map<String, String> map) {
        this.map = map;
    }

    @Override
    public String toString () {
        return "Test{" +
                "data=" + data +
                ", name='" + name + '\'' +
                ", first=" + (first == null ? null : Arrays.asList (first)) +
                ", second=" + second +
                ", map=" + map +
                '}';
    }

    public static void main (String[] args) throws Exception {
        XMLConverter converter = new XMLConverter ();
        FileInputStream in = new FileInputStream ("d:/tmp/test.xml");
        InputStreamReader isr = new InputStreamReader (in, "utf-8");
        BufferedReader reader = new BufferedReader (isr);
        StringBuilder builder = new StringBuilder ();
        String line;
        while ((line = reader.readLine ()) != null) {
            builder.append (line);
        }
        Test test = converter.cast (Test.class, builder.toString ());
        System.out.println (test);

        System.out.println ("-------------------------------------------- 华丽的分割线 ---------------------------------------------");

        String text = converter.cast (test);
        System.out.println (text);

        System.out.println ("-------------------------------------------- 我也是分割线 ---------------------------------------------");
        JSONConverter c = new JSONConverter ();
        text = c.cast (test);
        System.out.println (text);

        String[] a = {"a", "b", "c"};
        System.out.println (converter.cast (a));
    }
}